package myServlet;

import Smt.ServerImp.SmtUserServerImp;
import Smt.Util.SmtUser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class SmtUserRead extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		SmtUserServerImp ssi = new SmtUserServerImp();
		String update = request.getParameter("Update");
		if (update != null) {
			request.setAttribute("update", update);
		}
		String add = request.getParameter("add");
		if (add != null) {
			request.setAttribute("add", 3);
		}
		String error = (String) request.getAttribute("error");
		if (error != null) {
			request.setAttribute("error", error);
		}
		HttpSession session = request.getSession();
		String loginname = (String) session.getAttribute("loginname");
		String rms = (String) session.getAttribute("admin");
		ArrayList<SmtUser> list = null;
		String yeshu = request.getParameter("num");// 作为当前页
		if (yeshu == null)
			yeshu = "1";
		int ye = Integer.parseInt(yeshu);// 当前页
		int i = 3;// 每页内容
		String productName = (String) session.getAttribute("productName");// 用户名称
		if (productName == null) {
			productName = "";
		}
		session.setAttribute("productName", productName);
		String product = request.getParameter("productName");
		if (product == null) {
			product = "";
		}
		String b = request.getParameter("b");
		if (b == null) {
			productName = product;
		}
		list = ssi.read(ye, i, productName);// 当前页，每页内容
		int yenum = ssi.getYenum();// 总页数
		if (rms == null) {
			list = ssi.getSmtUser(loginname);
			yenum = 1;// 总页数
		} else {
			if (list.size() == 0) {
				request.setAttribute("nulls", "当前没有什么数据可以显示.");
			}
		}
		request.setAttribute("list", list);
		request.setAttribute("yenum", yenum);// 总页数
		request.setAttribute("num", yeshu);// 当前页
		request.getRequestDispatcher("SmtUser_list.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);

	}
}
